function renderBlog() {
	pjs.load("root",function(root) {
		pjs.get(root,'examples',function(examples) {// get the blog object, the list of the postings
			blog = examples.blog;
			document.getElementById('newPost').style.display='none';
			blogElement = document.getElementById("blogDiv");
			blogElement.innerHTML = ""; // clear it out
			Array.forEach(blog,function(post) { // iterate through the blog posts
				renderPost(post,blogElement.appendChild(document.createElement('div'))); // render each post in a new div
			});
		});
	});
}
function renderPost(post,postDiv,withComments) {
	var html = "<h2>" + post.title + "</h2>" 
		+ "<h4>" + post.created + "</h4><p>" + post.content + "</p>"; 
	if (withComments) {
		Array.forEach(post.comments,function(comment) { // iterate through the blog posts
			html += "<div class='comment'>" + comment.content + "</div><div class='author'>" 
				+ comment.author + " on " + comment.created + "</div>";
		});
	}
	document.getElementById('newComment').style.display=withComments ? 'block' : 'none';
	postDiv.innerHTML = html;
	postDiv.onclick = function() {
		renderPost(post,blogElement,true);
		currentPost = post;
	}
	postDiv.className="post";
}
function addBlogPost() {
	var newPost = {
		title : prompt("What would you like to title the new post?","new post"),
		content : document.getElementById('newPostContent').value,
		created : new Date(),
		comments : [] // an empty array of comments
	};
	blog.unshift(newPost); // add a new post to the top of the array using standard array functions
 	renderBlog();

}
function addCommentEntry() {
	var commentsDiv = document.getElementById("newComment")
	currentPost.comments.push({
		author : document.getElementById('newCommentAuthor').value,
		content : document.getElementById('newCommentContent').value,
		created : new Date()
	});
	renderPost(currentPost,blogElement,true);
}
renderBlog();